<?php
/**
 * 通用分页类
 * Author:Zero QQ:93133
 * ver 1.1.20100329 修正版
 */

class pageAjax{
 private $pstyle;
 public $firstRow=0;
 public $listRows=20;
 public $arg;
 public $cfg=array(
  'text'=>array('首页','上一页','下一页','尾页'),
  'pagesize'=>20,
  //htmltpl=> "list{\$page}.shtml",
  'listtpl'=>'[{$page}]',//仿搜索引擎页模板
  'tpl'=>array('t','条记录','f','p','l','n','e','s'),  //默认调用方式
   'total'=>0, //记录总数
  'attachtotal'=>false
  );
 public $totalpage;  //总页数
 
 function __construct(array $cfg){
 // dump($cfg);
  is_numeric($cfg['total'])|| exit('必须传入总记录数');
  $this->cfg=array_merge($this->cfg,$cfg);
  if($this->cfg['page']){  //传入当前页 适合html,ajax 需同时传入cfg[htmltpl]才起作用
   $this->arg['page']=$this->cfg['page'];
  }
  else{
   //分析URL中的当前页
   parse_str($this->cfg['arg']?$this->cfg['arg']:$_SERVER['QUERY_STRING'],$this->arg);
   $this->cfg['attachtotal']&&$this->arg['_total']=$this->cfg['total'];
   $this->arg['page']=is_numeric($this->arg['page'])?abs(ceil($this->arg['page'])):1;
  }
  //计算分页总数
  $this->cfg['pagesize']=$cfg['pagesize'];
  $this->firstRow=($this->arg['page']-1)*$this->cfg['pagesize'];
  //echo $this->cfg['total'].'/'.$cfg['pagesize'];
  $this->totalpage=ceil($this->cfg['total']/$this->cfg['pagesize']);
 }
 
 /**
  * 创建翻页链接
  *
  * @param int $page 页数
  * @return string 链接字符串
  *
  */
 
 private function buildurl($page){
  if($this->cfg['ajax']){  //ajax翻页
   return "javascript:{$this->cfg['ajax']}($page);";
  }
  elseif(!is_numeric($page)){ //javascript select控件翻页
   $arg=$this->arg;
   $arg['page']='{page}';
   return '\'?'.http_build_query($arg)."'.replace(escape('{page}'),$page)";
  }
  elseif(is_numeric($this->arg['page'])&&$this->cfg['htmltpl']){ //静态翻页
   
   return str_replace('{$page}',$page==1?null:$page,$this->cfg['htmltpl']);
  }
  else{ //PHP翻页
   $arg=$this->arg;
   $arg['page']=$page;
   return '?'.http_build_query($arg);
   
  }
  
 }
 
 /**
  * 文本输入框样式
  *
  */
 
 private function i(){
  $this->pstyle[]='跳转<input type="text" title="输入页数，按回车跳转" style="width:30px;text-align:center" onkeydown="javascript:if(event.keyCode==13){'.($this->cfg['ajax']?$this->buildurl('this.value'):"location.href={$this->buildurl('this.value')};return false;}").'" />页';
 }
 
 /**
  * 下拉列表
  *
  */
 
 private function s(){
  if($this->totalpage>300) return $this->i(); //大于300页，用输入方式代替
  $str='<select size="1" onchange="'.($this->cfg['ajax']?$this->buildurl('this.value'):"location.href={$this->buildurl('this.value')}").'">';
  for($i=1;$i<=$this->totalpage;$i++){
   $str.="<option value='$i'".($this->arg['page']==$i?' selected':null).">第{$i}页</option>";
  }
  $str.="</select>";
  $this->pstyle[]=$str;
 }
 
 /**
  * 首页样式
  *
  */
 
 private function f($show=true){
  $this->pstyle[]=$this->arg['page']==1&&$show? $this->cfg['text'][0]:"<a href=\"{$this->buildurl(1)}\">{$this->cfg['text'][0]}</a>";
 }
 
 /**
  * 尾页样式
  *
  */
 
 private function e($show=true){
  $this->pstyle[]=$this->arg['page']==$this->totalpage&&$show? $this->cfg['text'][3]:"<a href=\"{$this->buildurl($this->totalpage)}\">{$this->cfg['text'][3]}</a>";
  
 }
 
 /**
  * 上一页样式
  *
  */
 
 private function p($show=true){
  $this->pstyle[]=$this->arg['page']<2&&$show? $this->cfg['text'][1]:"<a href=\"{$this->buildurl($this->arg['page']-1)}\">{$this->cfg[text][1]}</a>";
 }
 
 /**
  * 下一页样式
  *
  */
 
 private function n($show=true){
  $this->pstyle[]=$this->arg['page']>=$this->totalpage&&$show? $this->cfg['text'][2]:"<a href=\"{$this->buildurl($this->arg['page']+1)}\">{$this->cfg['text'][2]}</a>";
 }
 
 
 
 /**
  * 放搜索引擎分页
  *
  * @param int $limit 最大左右偏移量之和
  * @param int $lstyle 样式
  * @return string 样式字符串
  *
  */
 private function l($limit=7,$lstyle=1){
  $a=floor($limit/2);  //左右偏移量
  $l=$this->arg['page']-$a;$r=$this->arg['page']+$a;  //默认偏移量
  $l<$a-1&&$r+=1-$l;//如果左不足
  
  if($r>$this->totalpage){//如果右不足
   $l-=$r-$this->totalpage;
   $r=$this->totalpage;
  }
  $l<1&&$l=1;
  //输出
  switch ($lstyle){
   case '0':
    for($i=$l;$i<=$r;$i++){
     $output.=' '.($this->arg['page']==$i? str_replace('{$page}',$i,$this->cfg['listtpl']):"<a href=\"{$this->buildurl($i)}\">".str_replace('{$page}',$i,$this->cfg['listtpl'])."</a>").' ';
    }
    break;
   case '1':
    $l>1&&$output="<a href=\"{$this->buildurl(1)}\">".str_replace('{$page}',1,$this->cfg['listtpl'])."</a>";
    $l>2&&$output.='...';
    for($i=$l;$i<=$r;$i++){
     $output.=' '.($this->arg['page']==$i? "<strong>$i</strong>":"<a href=\"{$this->buildurl($i)}\">".str_replace('{$page}',$i,$this->cfg['listtpl'])."</a>").' ';
    }
    $r<$this->totalpage-1&&$output.='...';
    $r<=$this->totalpage-1&&$output.="<a href=\"{$this->buildurl($this->totalpage)}\">".str_replace('{$page}',$this->totalpage,$this->cfg['listtpl'])."</a>";
  }
  $this->pstyle[]=$output;
 }
 /**
  * 取得记录总数
  *
  */
 private function t(){
  $this->pstyle[]=$this->cfg['total'];
 }
 /**
  * 取得总页数
  *
  */
 private function tp(){
  $this->pstyle[]=$this->totalpage;
 }
 
 /**
  * 创建完整分页样式
  *
  * @param string $space 各部分合并间隔符号
  * @return string 完整分页字符串
  *
  */
 public function show($space=' '){
  foreach($this->cfg['tpl'] as $arg){
   if(is_array($arg)){
    if(method_exists($this,$func=array_shift($arg))){
     call_user_func_array(array($this,$func),$arg);
    }
    else{
     $this->pstyle[]=$func;
    }
   }
   else{
    if(method_exists($this,$arg)){
     call_user_func(array($this,$arg));
    }
    else{
     $this->pstyle[]=$arg;
    }
   }
   
  }
  return implode($space,$this->pstyle);
 }
}
